package com.zmc.product.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.common.utils.StringUtils;
import com.zmc.common.vo.ProductSkuVo;
import com.zmc.product.infrastructure.bean.vo.GoodsEsVo;
import com.zmc.product.infrastructure.service.CommodityGoodsSkuService;
import com.zmc.product.infrastructure.bean.dto.GoodsSpuDto;
import com.zmc.product.infrastructure.bean.query.CommodityGoodsSkuQuery;
import com.zmc.product.infrastructure.bean.vo.GoodSkuVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.common.utils.PageUtils;

import com.zmc.product.infrastructure.dao.CommodityGoodsSkuDao;
import com.zmc.product.infrastructure.entity.CommodityGoodsSkuEntity;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author zmc
 */
@Service("commodityGoodsSkuService")
public class CommodityGoodsSkuServiceImpl extends ServiceImpl<CommodityGoodsSkuDao, CommodityGoodsSkuEntity> implements CommodityGoodsSkuService {


    @Override
    public Page<CommodityGoodsSkuEntity> pageCommodityGoodsSkuList(CommodityGoodsSkuQuery query) {
        return baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), getWrapper(query));
    }

    @Override
    public List<CommodityGoodsSkuEntity> selectCommodityGoodsSkuList(CommodityGoodsSkuQuery query) {
        return list(getWrapper(query));
    }

    @Override
    public GoodSkuVo getSkuInfoBySkuId(String skuId) {
        GoodSkuVo vo = new GoodSkuVo();
        CommodityGoodsSkuEntity skuEntity = getBySkuId(skuId);
        BeanUtils.copyProperties(skuEntity, vo);
        List<String> collect = Arrays.stream(skuEntity.getSaleProperties().split(",")).map(s -> {
            String[] split = s.split(":");
            return split[1];
        }).collect(Collectors.toList());
        vo.setSkuValueList(collect);
        return vo;
    }


    @Override
    public PageUtils pageGoodsSkuList(CommodityGoodsSkuQuery query) {
        int pageNum = (query.getPageNum() - 1) * query.getPageSize();
        List<GoodsSpuDto> list = baseMapper.pageGoodsSkuList(query.getSkuId(), query.getName(), pageNum, query.getPageSize());
        int count = baseMapper.countGoodsSkuList(query.getSkuId(), query.getName());
        return new PageUtils(list, (long) count, (long)query.getPageNum(), (long)query.getPageSize());
    }


    @Override
    public List<CommodityGoodsSkuEntity> getBySpuId(String spuId) {
        LambdaQueryWrapper<CommodityGoodsSkuEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommodityGoodsSkuEntity::getSpuId, spuId);
        return list(wrapper);
    }

    @Override
    public List<ProductSkuVo> collect(List<String> skuIds) {
        return baseMapper.selectBySkus(skuIds);
    }

    @Override
    public CommodityGoodsSkuEntity getBySkuId(String skuId) {
        LambdaQueryWrapper<CommodityGoodsSkuEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommodityGoodsSkuEntity::getSkuId, skuId);
        return getOne(wrapper);
    }

    @Override
    public List<GoodsEsVo> getBySkuIds(List<String> skuIds) {
        return baseMapper.getBySkuIds(skuIds);
    }

    @Override
    @Transactional
    public void removeBySpuId(String spuId) {
        baseMapper.removeBySpuId(spuId);
    }

    @Override
    @Transactional
    public void removeBySkuId(String skuId) {
        LambdaQueryWrapper<CommodityGoodsSkuEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommodityGoodsSkuEntity::getSkuId, skuId);
        remove(wrapper);
    }

    @Override
    public Long countBySpuId(String spuId) {
        LambdaQueryWrapper<CommodityGoodsSkuEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommodityGoodsSkuEntity::getSpuId, spuId);
        return count(wrapper);
    }


    private LambdaQueryWrapper<CommodityGoodsSkuEntity> getWrapper(CommodityGoodsSkuQuery query) {
        LambdaQueryWrapper<CommodityGoodsSkuEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(query.getSpuId()), CommodityGoodsSkuEntity::getSpuId, query.getSpuId())
                .eq(StringUtils.isNotEmpty(query.getSkuId()), CommodityGoodsSkuEntity::getSkuId, query.getSkuId());
        return wrapper;
    }






}